package cn.edu.cxcyzx.controller;


import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import cn.edu.cxcyzx.entity.Gitee;
import cn.edu.cxcyzx.supervisor.ResultDo;
import cn.edu.cxcyzx.supervisor.ServiceFit;
import cn.edu.cxcyzx.util.CatchUtil;
import cn.edu.cxcyzx.util.GiteeOauth;
import cn.edu.cxcyzx.util.HttpClientUtil;
import cn.edu.cxcyzx.util.MsgJSON;
/**
 * git操作 相关接口
 * @author zxl
 * @date   2018年6月17日下午8:28:01
 * @描述:
 *
 */
@Controller
@RequestMapping("/git")
public class GitController {
	
	@Resource
	ServiceFit serviceFit;
	/**
	 * 转发到--> gitGuide页面
	 * @param role
	 * @return
	 */
	@RequestMapping("/gitGuide")
	public ModelAndView toGitGuide(int role){
		
		ModelAndView modelAndView=new ModelAndView();
		if(role==2){
			modelAndView.setViewName("student/gitGuide");
		}else if(role==3){
			modelAndView.setViewName("teacher/gitGuide");
		}
		return modelAndView;
		
	}
	/**
	 * gitee 获取 AccessToken 认证
	 * @param code
	 * @return
	 */
	@RequestMapping("/gitee_oauth")
	@ResponseBody
	public String gitee_oauth(String code,HttpSession session){
		String roleID=(String) session.getAttribute("roleID");
		Object o=null;
		if(roleID!=null){
			System.out.println("key:"+"gitee_"+roleID);
			o=CatchUtil.getValue("gitee_"+roleID);
		}
		if(o==null){
			System.out.println("模拟请求");
			return "模拟请求成功";
		}
		System.out.println("roleID:"+roleID);
		System.out.println("code:"+code);
		Gitee g=(Gitee) o;
		System.out.println("g:"+g.toString());
		Gitee gitee=new GiteeOauth(code,g.getClientId(),g.getClientSecret()).doPost();
		if(gitee==null){
			return "处理失败,服务器繁忙，请一会再操作！";
		}else{
			gitee.setCode(code);
			gitee.setUserId(roleID);
			gitee.setUserName(g.getUserName());
			gitee.setClientId(g.getClientId());
			gitee.setClientSecret(g.getClientSecret());
			serviceFit.getGiteeService().insertGitee(gitee);
			System.out.println("success*********gitee:"+gitee.toString());
			return "授权成功";
		}
	}
	/**
	 * 判断用户是否gitee授权
	 * @param session
	 * @return
	 */
	@RequestMapping("/getGitee")
	@ResponseBody
	public MsgJSON getGitee(HttpSession session){
		String userId=(String) session.getAttribute("roleID");
		Gitee gitee=serviceFit.getGiteeService().getGitee(userId);
		if(gitee==null){
			return MsgJSON.fail().add("msg", "未授权");
		}else{
			return MsgJSON.success();
		}
	}
	/**
	 * 将用户页面输入的信息存入缓存
	 * @param gitee
	 * @return
	 */
	@RequestMapping("/insertGitee")
	@ResponseBody
	public MsgJSON insertGitee(Gitee gitee,HttpSession session){
		String userId=(String) session.getAttribute("roleID");
		gitee.setUserId(userId);
		System.out.println("插入gitee:"+gitee.toString());
		System.out.println("插入前size:"+CatchUtil.getSize());
		CatchUtil.setValue("gitee_"+userId, gitee);
		System.out.println("插入后size:"+CatchUtil.getSize());
		CatchUtil.printf();
		return MsgJSON.success();
		
	}
	/**
	 * 获取gitee用户的主页
	 * @param userId
	 * @param session
	 * @return
	 */
	@RequestMapping("/getGiteePage")
	@ResponseBody
	public MsgJSON getGiteePage(String userId,HttpSession session){
		System.out.println("getGiteePage:"+userId);
		ResultDo<JSONObject> resultDo=serviceFit.getGiteeService().gitee_userInfo(userId);
		if(resultDo.isSuccess()){
			return MsgJSON.success().add("url",resultDo.getResult().getString("html_url"));
		}else{
			return MsgJSON.fail().add("msg", resultDo.getMessage());
		}
	}
	/**
	 * 判断用户有没有在gitee上加入此项目
	 * @param userId
	 * @param projectId
	 * @return
	 */
	@RequestMapping("/isInGiteePro")
	@ResponseBody
	public MsgJSON isInGiteePro(String userId,String projectId){
		if(userId==null||"".equals(userId.trim())||projectId==null||"".equals(projectId.trim())){
			return MsgJSON.fail().add("msg", "参数异常");
		}
		ResultDo<String> resultDo=serviceFit.getGiteeService().isInGiteePro(userId, Integer.parseInt(projectId));
		if(resultDo.isSuccess()){
			return MsgJSON.success();
		}else{
			return MsgJSON.fail().add("msg", resultDo.getMessage());
		}
	}
	/**
	 * 判断分支是否存在
	 * @param projectId
	 * @param branch
	 * @return
	 */
	@RequestMapping("/isHaveBranch")
	@ResponseBody
	public MsgJSON isHaveBranch(String projectId,String branch){
		if(projectId==null||"".equals(projectId.trim())){
			return MsgJSON.fail().add("msg", "参数错误");
		}
		ResultDo<String> resultDo=serviceFit.getGiteeService().isHaveBranch(Integer.parseInt(projectId), branch);
		if(resultDo.isSuccess()){
			return MsgJSON.success();
		}else{
			return MsgJSON.fail().add("msg", resultDo.getMessage());
		}
	}
	
	/**
	 * 弹出展示gtiee提交代码信息的页面
	 * @param userId
	 * @param projectId
	 * @return
	 */
	@RequestMapping("/showGiteePush")
	public ModelAndView showGiteePush(String userId,String projectId){
		ModelAndView modelAndView=new ModelAndView("teacher/userGiteePush");
		modelAndView.addObject("userId", userId);
		modelAndView.addObject("projectId", projectId);
		return modelAndView;
	}
	/**
	 * 获取用户gitee中代码提交信息
	 * @param userId
	 * @param projectId
	 * @return
	 */
	@RequestMapping("/getUserGiteePush")
	public MsgJSON getUserGiteePush(String userId,String projectId){
		return null;
		
	}
	

}
